package system.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import system.common.Arguments;
import system.common.Constants;
import system.dao.DB6_RedcastleDao;
import system.model.DB6_RedcastleDto;
import system.util.FileUtil;

@Service
public class DB6_RedcastleService {
	@Autowired
	private DB6_RedcastleDao db6_RedcastleDao;

	public void redcastle_01(Arguments arguments) throws IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getParameter2() + "_" + arguments.getParameter3();
		String historyFileName = Constants.REDCASTLE_01_History + Constants.REDCASTLE_01 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.REDCASTLE_01_Current + Constants.REDCASTLE_01;
		
		// check for directory
		FileUtil.directoryCheck(Constants.REDCASTLE_01_History);
		FileUtil.directoryCheck(Constants.REDCASTLE_01_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.REDCASTLE_01, Constants.REDCASTLE_01_Current);

		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		// rs start
		long start = System.currentTimeMillis();
		
		List<DB6_RedcastleDto> redcastle_01_list = db6_RedcastleDao.redcastle_01_list(arguments);

		long end = System.currentTimeMillis();
		Constants.rsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		ExportService exportService = new ExportService();
		exportService.OutputList(redcastle_01_list, bufferedWriter, historyFileName, "_0001", 0, Constants.REDCASTLE_01);
		
		end = System.currentTimeMillis();
		Constants.fileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(Constants.REDCASTLE_01_History, Constants.REDCASTLE_01 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		Constants.fileCopyTime = (end - start) / 1000f;
		// file-copy end
	}

	public void redcastle_02(Arguments arguments) throws IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getParameter2() + "_" + arguments.getParameter3();
		String historyFileName = Constants.REDCASTLE_01_History + Constants.REDCASTLE_01 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.REDCASTLE_01_Current + Constants.REDCASTLE_01;
		
		// check for directory
		FileUtil.directoryCheck(Constants.REDCASTLE_01_History);
		FileUtil.directoryCheck(Constants.REDCASTLE_01_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.REDCASTLE_02, Constants.REDCASTLE_02_Current);

		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		// rs start
		long start = System.currentTimeMillis();
		
		List<DB6_RedcastleDto> redcastle_02_list = db6_RedcastleDao.redcastle_02_list(arguments);

		long end = System.currentTimeMillis();
		Constants.rsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		ExportService exportService = new ExportService();
		exportService.OutputList(redcastle_02_list, bufferedWriter, historyFileName, "_0001", 0, Constants.REDCASTLE_02);
		
		end = System.currentTimeMillis();
		Constants.fileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(Constants.REDCASTLE_02_History, Constants.REDCASTLE_02 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		Constants.fileCopyTime = (end - start) / 1000f;
		// file-copy end
	}

	public void redcastle_03(Arguments arguments) throws IOException {
		String fileNameSuffix = arguments.getParameter1() + "_" + arguments.getParameter2() + "_" + arguments.getParameter3();
		String historyFileName = Constants.REDCASTLE_03_History + Constants.REDCASTLE_03 + Constants.fileNamePrefix + fileNameSuffix;
		String currentFileName = Constants.REDCASTLE_03_Current + Constants.REDCASTLE_03;
		
		// check for directory
		FileUtil.directoryCheck(Constants.REDCASTLE_03_History);
		FileUtil.directoryCheck(Constants.REDCASTLE_03_Current);
		
		// delete to current file
		FileUtil.deleteFile(Constants.REDCASTLE_03, Constants.REDCASTLE_03_Current);

		File file = null;
		BufferedWriter bufferedWriter = null;
		Constants.rsTime = 0;
		Constants.fileMakeTime = 0;
		Constants.fileCopyTime = 0;
		Constants.totalRecord = 0;
		
		// rs start
		long start = System.currentTimeMillis();
		
		List<DB6_RedcastleDto> redcastle_03_list = db6_RedcastleDao.redcastle_03_list(arguments);

		long end = System.currentTimeMillis();
		Constants.rsTime = (end - start) / 1000f;
		// rs end
		// file-make start
		start = System.currentTimeMillis();
		
		file = new File(historyFileName + "_0001" + FileUtil.genFileNumber(0) + Constants.fileNameExtension);
		bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));

		ExportService exportService = new ExportService();
		exportService.OutputList(redcastle_03_list, bufferedWriter, historyFileName, "_0001", 0, Constants.REDCASTLE_03);
		
		end = System.currentTimeMillis();
		Constants.fileMakeTime = (end - start) / 1000f;
		// file-make end
		// file-copy start
		start = System.currentTimeMillis();
		
		FileUtil.copyBytes(Constants.REDCASTLE_03_History, Constants.REDCASTLE_03 + Constants.fileNamePrefix + fileNameSuffix, currentFileName, Constants.fileNameExtension);

		end = System.currentTimeMillis();
		Constants.fileCopyTime = (end - start) / 1000f;
		// file-copy end
	}
}
